setwd("~/lab3r")
library("RSNNS")                                                                    #load the package RSNNS

iris.shuffled <- iris[sample(1:nrow(iris),nrow(iris)),]                             #shuffle the lines of the dataset

iris.inputs <- iris.shuffled[,1:4]                                                  #copy the input columns in a separate variable
iris.targets <- decodeClassLabels(iris.shuffled[,5], valTrue=0.95, valFalse=0.05)   #decode the classes into a binary representation

iris.split <- splitForTrainingAndTest(iris.inputs, iris.targets, ratio=0.15)        #split the dataset in 2 parts: training and test
iris.normalized <- normTrainingAndTestSet(iris.split)                               #normalize both parts (training and test)

#Training: find the parameters of the model (weights)
model <- mlp(   x=iris.normalized$inputsTrain,                                           #input data for training
                y=iris.normalized$targetsTrain,                                          #output data (targets) for training
                size=5,                                                             #number of neurons in the hidden layer
                learnFunc="Std_Backpropagation",                                    #type of learning
                learnFuncParams=c(0.1),                                             #paramenters of the learning function (eta)
                maxit=1000,                                                         #maximum number of iterations
                inputsTest=iris.normalized$inputsTest,                                   #input data for testing
                targetsTest=iris.normalized$targetsTest)                                 #output data (targets) for testing

plotIterativeError(model)                                                           #plots the evolution of the error through the training process

summary(model)                                                                      #prints a summary of the model
weightMatrix(model)                                                                 #shows the matrix of weigths of the neural network
#black: training dataset, red: test dataset